﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FrbaBus.Core;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace FrbaBus.Abm_Micro
{
    
    public partial class Distribucion_de_Butacas : Form
    {
        public static bool IsOdd(int value)
        {
            return value % 2 != 0;
        }
        public AltaMicro parent = null;
        public ModificarMicro2 parent2 = null;
        public string s1;
        public bool Alta;
        public int valor = 0;
        public Distribucion_de_Butacas(Form callingForm, bool Alta2)
        {
            if (Alta2)
                parent = callingForm as AltaMicro;
            else
                parent2 = callingForm as ModificarMicro2;

            Alta = Alta2;
            InitializeComponent();
        }

        public string update1;
        public string update2;
        public string update3;

        private void button1_Click(object sender, EventArgs e)
        {
            string cantButacas;
            if (Alta)
                cantButacas = this.parent.numericUpDown2.Text;
            else
                cantButacas = this.parent2.numericUpDown2.Text;

            int i = 0;
            if (!Alta)
                i = valor;

            if (i < Convert.ToInt16(cantButacas))
            {
                s1 = s1 + "(" + dataGridView1.Rows[i].Cells[0].Value.ToString() + ",'"
                        + dataGridView1.Rows[i].Cells[1].Value.ToString() + "',"
                        + dataGridView1.Rows[i].Cells[2].Value.ToString() + ")";

                for (i++; i < Convert.ToInt16(cantButacas); i++)
                {


                    s1 = s1 + ", (" + dataGridView1.Rows[i].Cells[0].Value.ToString() + ",'"
                    + dataGridView1.Rows[i].Cells[1].Value.ToString() + "',"
                    + dataGridView1.Rows[i].Cells[2].Value.ToString() + ")";


                }
            }
            i = 0;
            if (!Alta)
            {
                if ( valor > 0 )
                {
                    update1 = "update ALL_IN.Butaca SET Piso =(CASE WHEN ID_Butaca =" + dataGridView1.Rows[i].Cells[0].Value.ToString() +
                        " THEN " + dataGridView1.Rows[i].Cells[2].Value.ToString();
                    update2 = ", Tipo_Butaca = ( CASE WHEN ID_Butaca ="+ dataGridView1.Rows[i].Cells[0].Value.ToString() +
                        " THEN '" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "'";
                    update3 = "AND ( ID_Butaca=" + dataGridView1.Rows[i].Cells[0].Value.ToString();

                    for (i++; i < valor; i++)
                    {
                        update1 = update1+ " WHEN ID_Butaca =" + dataGridView1.Rows[i].Cells[0].Value.ToString() +
                            " THEN " + dataGridView1.Rows[i].Cells[2].Value.ToString();
                        update2 = update2 + " WHEN ID_Butaca =" + dataGridView1.Rows[i].Cells[0].Value.ToString() +
                            " THEN '" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "'";
                        update3 = update3 + " OR ID_Butaca=" + dataGridView1.Rows[i].Cells[0].Value.ToString();
                    }
                    update1 = update1 + " END )";
                    update2 = update2 + " END )";
                    update3 = update3 + " )";

                }

            }


            if (Alta)
                parent.butacas = string.Copy(s1);
            else
            {
                if (s1 != null)
                    parent2.butacasinsert = string.Copy(s1);
                else
                    parent2.butacasinsert = null;

                parent2.cantButacasPrevias = valor;

                if (update1 != null && update2 != null)
                    parent2.butacasupdate = string.Copy(update1) + string.Copy(update2);
                else
                    parent2.butacasupdate = null;
                if (update3 != null)
                    parent2.butacasupdate2 = string.Copy(update3);
                else
                    parent2.butacasupdate2 = null;
            }

            this.Close();
        }

        
        private void Distribucion_de_Butacas_Load(object sender, EventArgs e)
        {

                
            
            int i = 0;
            if ( Alta )
            {
                parent.especificarButacas = true;
                string cantButacas = this.parent.numericUpDown2.Text;
                int NumRows = Convert.ToInt16(cantButacas);
                string[,] Butacas = new string[3, NumRows];


                for (i = 0; i < Convert.ToInt16(cantButacas); i++)
                {
                    dataGridView1.Rows.Add(1);
                    dataGridView1.Rows[i].Cells[0].Value = i;

                    if (IsOdd(i))
                        dataGridView1.Rows[i].Cells[1].Value = "Pasillo";
                    else
                        dataGridView1.Rows[i].Cells[1].Value = "Ventanilla";

                    if (i > 24)
                        dataGridView1.Rows[i].Cells[2].Value = "2";
                    else
                        dataGridView1.Rows[i].Cells[2].Value = "1";

                }
            }
            else
            {
                
                
                parent2.especificarButacas = true;

                DBManager db = new DBManager();
                SqlCommand consulta = new SqlCommand("select count (*) from ALL_IN.Butaca where Patente= '" + parent2.textBox10.Text + "'");
                SQLResponse r = db.executeQuery(consulta);
                valor = (int)r.result.Rows[0][0];
                int b;

                if (valor > 0)
                {
                    SqlCommand consulta2 = new SqlCommand("select ID_Butaca,Tipo_Butaca,Piso from ALL_IN.Butaca where Patente= '" 
                        + parent2.textBox10.Text + "'");
                    SQLResponse r2 = db.executeQuery(consulta2);
                    for ( i = 0; i < valor; i++)
                    {
                        dataGridView1.Rows.Add(r2.result.Rows[i][0].ToString(),r2.result.Rows[i][1].ToString(),r2.result.Rows[i][2].ToString());

                    }

                    SqlCommand consultaJ = new SqlCommand("select COUNT(DISTINCT ID_Butaca) from ALL_IN.Disponibilidad_Butaca d where d.Mic_Patente = '" +
                       parent2.textBox10.Text + "' AND Fecha_Salida > (select * from ALL_IN.Fecha_Actual)" +
                    "AND NOT EXISTS ( SELECT * FROM ALL_IN.Cancelacion c WHERE c.Nro_Pasaje = d.Nro_Pasaje )");
                    SQLResponse rJ = db.executeQuery(consultaJ);
                    int valor2 = (int)rJ.result.Rows[0][0];
                    if ( valor2 > 0)
                    {
                        SqlCommand consultaK = new SqlCommand("select ID_Butaca from ALL_IN.Disponibilidad_Butaca d where d.Mic_Patente = '" + parent2.textBox10.Text +
                            "' AND Fecha_Salida > (select * from ALL_IN.Fecha_Actual)" +
                            "   AND NOT EXISTS ( SELECT * FROM ALL_IN.Cancelacion c WHERE c.Nro_Pasaje = d.Nro_Pasaje )" +
                            "GROUP BY ID_Butaca,d.Mic_Patente ORDER BY ID_Butaca");

                        SQLResponse rK = db.executeQuery(consultaK);
                        i = 0;
                        for (int k = 0; k < valor2 && i < valor; k++)
                        {
                            for (; k < valor2 && i < valor && Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value) <= Convert.ToInt32(rK.result.Rows[k][0]); i++)
                            {

                                if (Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value) == Convert.ToInt32(rK.result.Rows[k][0]))
                                {
                                    DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[1];
                                    cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                                    cell = (DataGridViewComboBoxCell)dataGridView1.Rows[i].Cells[2];
                                    cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.SystemColors.Control;
                                    

                                    dataGridView1.Rows[i].ReadOnly = true;
                                }

                            }
                        }



                    }




                }
                for (b = valor; b < Convert.ToInt16(this.parent2.numericUpDown2.Text); b++)
                {
                    dataGridView1.Rows.Add(1);
                    dataGridView1.Rows[b].Cells[0].Value = b;

                    if (IsOdd(b))
                        dataGridView1.Rows[b].Cells[1].Value = "Pasillo";
                    else
                        dataGridView1.Rows[b].Cells[1].Value = "Ventanilla";

                    if (b > 24)
                        dataGridView1.Rows[b].Cells[2].Value = "2";
                    else
                        dataGridView1.Rows[b].Cells[2].Value = "1";




                }
 



            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
